package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.MessageDAO;

import beans.MessageBean;
import beans.UserBean;

/**
 * Servlet implementation class SendMessageServlet
 */
@WebServlet("/SendMessageServlet")
public class SendMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SendMessageServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserBean sender = (UserBean)session.getAttribute("currentSessionUser");
		UserBean receiver = (UserBean)session.getAttribute("visitingUser");
		String body = request.getParameter("messagebody");
		Integer replyTo = (Integer)session.getAttribute("replyingTo");
		boolean isPrivate;
		String select[] = request.getParameterValues("isPrivate");

		if (select != null)
			isPrivate = true;
		else
			isPrivate = false;

		MessageBean insertingMessage = new MessageBean(sender.getEmail(), receiver.getEmail(), isPrivate, body, replyTo);

		System.out.println("Inserting message from "+sender+" to "+receiver);

		MessageDAO.sendMessage(insertingMessage);
		response.sendRedirect("VisitServlet?visitid=" + receiver.getEmail());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
